package com.situ.mall.admin.controller;


import com.situ.mall.admin.pojo.vo.PermissionVO;
import com.situ.mall.admin.service.IPermissionService;
import com.situ.mall.common.util.Result;
import com.situ.mall.pojo.entity.admin.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2024-11-29
 */
@RestController
@RequestMapping("/admin/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/selectPermissionTree")
    public Result<List<PermissionVO>> selectPermissionTree() {
        List<PermissionVO> list = permissionService.selectPermissionTree();
        return Result.ok(list);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return Result.ok("更新成功");
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        permissionService.removeById(id);
        return Result.ok("删除成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) {
        permissionService.save(permission);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result<Permission> selectById(@PathVariable Integer id) {
        Permission permission = permissionService.getById(id);
        return Result.ok(permission);
    }

    @GetMapping("/selectAssignedPermission/{roleId}")
    public Result selectAssignedRole(@PathVariable("roleId") Long roleId) {
        Map<String, Object> map = permissionService.selectAssignedRole(roleId);
        return Result.ok(map);
    }

    @PostMapping("/assignPermission")
    public Result assignPermission(Long roleId, Long[] permissionIds) {
        permissionService.assignPermission(roleId, permissionIds);
        return Result.ok("分配成功");
    }
}

